package com.hula.onion.book.server.spider.platforms.iqianjin;

import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.model.AccountInfo;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class IQianJinSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(4);

        map.put("name", form.getUserName());
        map.put("password", Base64Utils.encodeToString(form.getPassword().getBytes()));
        map.put("code", form.getTicket());
        map.put("rememberMe", "on");

        return map;
    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public AccountInfo parseAccountBalance(String html) {

        AccountInfo accountInfo = new AccountInfo();

        if (!StringUtils.isEmpty(html)) {

            JSONObject bean = JSONObject.parseObject(html).getJSONObject("bean");

            String amount = bean.getString("totalAssetsDesc");

            accountInfo.setAmount(new BigDecimal(amount));
        }


        return accountInfo;
    }


}
